import * as echarts from '../../ec-canvas/echarts';
const apiserverUrl = require('../../config').apiserverUrl;
const UserExamResultUrl = require('../../config').UserExamResultUrl;

const app = getApp();

Page({
  onShareAppMessage: function (res) {

  },
  data: {
    loading: true,
    papersid: 0,
    userInfo: [],
    examInfo: [],
    haswq: false
  },
  onReady() {

  },
  onLoad: function (options) {

    this.setData({ userInfo: app.globalData.userInfo, papersid: options.papersid });
    //获取用户考试结果
    wx.request({
      method: "post",//请求数据用get，添加数据post，修改数据put
      url: apiserverUrl,
      data: {
        method: "GET", url: UserExamResultUrl, data: {
          papersid: this.data.papersid,
          userid: this.data.userInfo.userid
        }
      },
      header: {
        "Content-Type": "application/json"
      },
      success: res => {
        if (res.data.IsSuccess) {
          //初始化图表
          //res.data.Data.point//考试分数
          this.setData({
            examInfo: res.data.Data.examinfo,
            haswq: res.data.Data.haswq,
            loading: false
          });

          this.ecGaugeComponent = this.selectComponent('#mychart-dom-gauge');
          this.ecGaugeComponent.init((canvas, width, height) => {
            // 获取组件的 canvas、width、height 后的回调函数
            // 在这里初始化图表
            const chart = echarts.init(canvas, null, {
              width: width,
              height: height
            });
            this.initChart(chart);
            // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
            this.chart = chart;
            // 注意这里一定要返回 chart 实例，否则会影响事件处理等
            return chart;
          });
        }
      }
    });
  },
  initChart: function (chart) {
    const option = {
      backgroundColor: "#ffffff",
      color: ["#37A2DA", "#32C5E9", "#67E0E3"],
      series: [{
        name: '考试成绩',
        type: 'gauge',
        detail: {
          formatter: '{value}'
        },
        axisLine: {
          show: true,
          lineStyle: {
            width: 30,
            shadowBlur: 0,
            color: [
              [0.9, '#c1272d'],
              [1, '#c39c61']
            ]
          }
        },
        data: [{
          value: this.data.examInfo.point,
          name: "",
        }]

      }]
    };
    chart.setOption(option);
  }
});
